package cn.jxau.fukin.service.impl;

import static cn.jxau.fukin.util.ConstantUtil.SUCCESS;
import static cn.jxau.fukin.util.PKUtil.getPrimaryKey;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jxau.fukin.dao.TradeMapper;
import cn.jxau.fukin.dao.UserMapper;
import cn.jxau.fukin.model.Trade;
import cn.jxau.fukin.model.TradeExample;
import cn.jxau.fukin.model.TradeExample.Criteria;
import cn.jxau.fukin.model.TradeUser;
import cn.jxau.fukin.model.User;
import cn.jxau.fukin.model.UserExample;
import cn.jxau.fukin.service.ITradeService;

@Service("tradeService")
public class TradeService implements ITradeService {
	@Autowired
	TradeMapper tradeMapper;
	@Autowired
	UserMapper userMapper;

	// 添加交易记录
	public String addTradeRecord(User user, User other, BigDecimal money, String type) {
		Trade trade = new Trade();
		trade.setTradeId(getPrimaryKey());
		trade.setuId(user.getuId());
		trade.setAcceptId(other.getuId());
		trade.setTradeAmount(money);
		trade.setTradeType(type);
		trade.setTradeTime(new Date());
		tradeMapper.insert(trade);
		return SUCCESS;
	}

	// 按条件查询
	public List<Trade> selectTradeReord(Trade trade) {
		if (trade == null)
			return null;
		TradeExample tradeExample = new TradeExample();
		Criteria criteria = tradeExample.createCriteria();
		judge(trade, criteria);
		return tradeMapper.selectByExample(tradeExample);
	}

	// 按主键查询
	public Trade selectTradeByPrimarkey(String tradeId) {
		return tradeMapper.selectByPrimaryKey(tradeId);
	}

	void judge(Trade trade, Criteria criteria) {
		if (trade.getAcceptId() != null && !"".equals(trade.getAcceptId())) {
			criteria.andAcceptIdEqualTo(trade.getAcceptId());
		}
		if (trade.getTradeAmount() != null && !"".equals(trade.getTradeAmount())) {
			criteria.andTradeAmountEqualTo(trade.getTradeAmount());
		}
		if (trade.getTradeId() != null && !"".equals(trade.getTradeId())) {
			criteria.andTradeIdEqualTo(trade.getTradeId());
		}
		if (trade.getTradeTime() != null) {
			criteria.andTradeTimeEqualTo(trade.getTradeTime());
		}
		if (trade.getTradeType() != null && !"".equals(trade.getTradeType())) {
			criteria.andTradeTypeEqualTo(trade.getTradeType());
		}
		if (trade.getuId() != null && !"".equals(trade.getuId())) {
			criteria.andUIdEqualTo(trade.getuId());
		}
	}

	@Override
	public List<TradeUser> selectTradeByUser(User user) {
		UserExample userExample = new UserExample();
		userExample.createCriteria().andUNameEqualTo(user.getuName());
		List<User> user01 = userMapper.selectByExample(userExample);
		TradeExample tradeExample = new TradeExample();
		tradeExample.createCriteria().andUIdEqualTo(user01.get(0).getuId());
		List<Trade> tradeList = tradeMapper.selectByExample(tradeExample);
		ArrayList<TradeUser> tradeUserList = new ArrayList<TradeUser>();
		for (Trade trade : tradeList) {
			TradeUser tradeUser = new TradeUser();
			tradeUser.setUser(user01.get(0));
			User user2 = userMapper.selectByPrimaryKey(trade.getAcceptId());
			tradeUser.setAcceptUser(user2);
			tradeUser.setTrade(trade);
			tradeUserList.add(tradeUser);
		}
		return tradeUserList;
	}

}
